<?php

namespace App\Http\Controllers\Mobile;

use App\Models\BlockItem;
use App\Models\Shop;
use Illuminate\Support\Facades\DB;

class ShopController extends BaseController
{
    //
    public function index()
    {

        $focusImages = BlockItem::where('block_id', 6)->orderBy('displayorder')->get();
        $brands = BlockItem::where('block_id', 7)->limit(8)->orderBy('displayorder')->get();
        return $this->view('mobile.shop.index', compact('focusImages', 'brands'));
    }

    public function getjson()
    {
        $offset = $this->request->input('offset');
        $count  = $this->request->input('count');
        $offset = $offset ? $offset : 0;
        $count  = $count ? $count : 20;

        $lng = $lat = 0;
        $location = $this->request->input('location');
        if ($location) {
            $point = explode(',', $location);
            $lng = floatval($point[0]);
            $lat = floatval($point[1]);
        } else {
            $lat = floatval($this->request->input('lat'));
            $lng = floatval($this->request->input('lng'));
        }

        $shops = [];
        Shop::where('item_count', '>', 2)->opening()
            ->offset($offset)->limit($count)->orderByDesc('total_sold')->get()
            ->map(function ($shop) use (&$shops, $lat, $lng){
                $shop->url = mobile_shop_url($shop->shop_id);
                if ($shop->lng && $shop->lat) {
                    $shop->distance = distance(getDistance($lat, $lng, $shop->lat, $shop->lng));
                }else{
                    $shop->distance = '';
                }

                $shop->logo = image_url($shop->logo);
                if ($shop->total_sold > 10000) {
                    $shop->total_sold = round($shop->total_sold/10000, 2).'万';
                }
                $shop->items = [];
                $shops[$shop->shop_id] = $shop->toArray();
            });

        $shopIds = array_keys($shops);
        $prefix = DB::getTablePrefix();
        $query = DB::table(DB::raw($prefix."item as i"))
            ->where(DB::raw("(select count(*) from {$prefix}item where shop_id=i.shop_id and itemid<i.itemid)"),'<', 3);

        $query->whereIn('shop_id', $shopIds)->orderByDesc('itemid')->get()->map(function ($item) use (&$shops){
            $item->thumb = image_url($item->thumb);
            $item->image = image_url($item->image);
            $item->url = mobile_item_url($item->itemid);
            $shops[$item->shop_id]['items'][] = $item;
        });

        return ajaxReturn(['items'=>array_values($shops)]);
    }

    public function viewshop($shop_id)
    {
        Shop::where('shop_id', $shop_id)->incrementViews();
        $shop = Shop::find($shop_id);

        $orderby = $this->request->input('orderby');
        $orderby = $orderby ? $orderby : 'sold';
        $this->assign([
            'shop'=>$shop,
            'shop_id'=>$shop_id,
            'items'=>$shop->items()->onSale()->orderByDesc($orderby == 'sold'? 'sold' : 'itemid')->get(),
            'orderby'=>$orderby
        ]);

        return $this->view('mobile.shop.viewshop');
    }

    public function profile($shop_id)
    {
        $shop = Shop::find($shop_id);

        return $this->view('mobile.shop.profile', compact('shop'));
    }
}
